package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.OtherRecord;
import cn.fintecher.pangolin.service.business.model.request.AddWorkOrderRequest;
import cn.fintecher.pangolin.service.business.model.request.FindWorkOrderRequest;
import cn.fintecher.pangolin.service.business.model.request.ModifyWorkOrderRequest;
import cn.fintecher.pangolin.service.business.service.WorkOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Auther: xiaqun
 * @Description: 工单相关接口
 * @Date: 14:40 2019/10/12
 */

@RestController
@RequestMapping("/api/workOrderController")
@Api(value = "工单相关接口", description = "工单相关接口")
public class WorkOrderController extends BaseController {
    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 添加工单
     */
    @ApiOperation(value = "添加工单", notes = "添加工单")
    @PostMapping("/addWorkOrder")
    public ResponseEntity<Void> addWorkOrder(@Valid @RequestBody AddWorkOrderRequest request,
                                             @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add work order,{}", request);
        workOrderService.addWorkOrder(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询工单
     */
    @ApiOperation(value = "查询工单", notes = "查询工单")
    @GetMapping("/findAllWorkOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<OtherRecord>> findAllWorkOrder(FindWorkOrderRequest request, Pageable pageable) {
        log.info("REST request to find all work order,{}", request);
        Page<OtherRecord> page = workOrderService.findAllWorkOrder(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 修改工单
     */
    @ApiOperation(value = "修改工单", notes = "修改工单")
    @PutMapping("/modifyWorkOrder")
    public ResponseEntity<Void> modifyWorkOrder(@RequestBody ModifyWorkOrderRequest request,
                                                @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify work order,{}", request);
        workOrderService.modifyWorkOrder(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除工单
     */
    @ApiOperation(value = "删除工单", notes = "删除工单")
    @DeleteMapping("/deleteWorkOrder")
    public ResponseEntity<Void> deleteWorkOrder(@RequestParam @ApiParam(value = "工单ID", required = true) String id) {
        log.info("REST request to delete work order,{}", id);
        workOrderService.deleteWorkOrder(id);
        return ResponseEntity.ok().build();
    }
}
